# Estimate the conditional effects of economic and social polarization separately.
# See Appendix O.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

all(c("populist_present", "economic_polarization", "social_polarization") %in% colnames(president_df)) # TRUE

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27





##################
# winners + love #
##################

winner_love_economic <- brm(loveprop ~ post*populist_present + post*economic_polarization
                            + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                            + Xl + Xr 
                            + (1 + Xl + Xr | party_election),
                            data=winners,
                            warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love_economic)
summary(winner_love_economic)

winner_love_social <- brm(loveprop ~ post*populist_present + post*social_polarization
                          + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                          + Xl + Xr 
                          + (1 + Xl + Xr | party_election),
                          data=winners,
                          warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love_social)
summary(winner_love_social)




###################
# winners + angry #
###################

winner_angry_economic <- brm(angryprop ~ post*populist_present + post*economic_polarization
                             + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                             + Xl + Xr 
                             + (1 + Xl + Xr | party_election),
                             data=winners,
                             warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry_economic)
summary(winner_angry_economic)

winner_angry_social <- brm(angryprop ~ post*populist_present + post*social_polarization
                           + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                           + Xl + Xr 
                           + (1 + Xl + Xr | party_election),
                           data=winners,
                           warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry_social)
summary(winner_angry_social)




#################
# losers + love #
#################

loser_love_economic <- brm(loveprop ~ post*populist_present + post*economic_polarization
                           + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                           + Xl + Xr 
                           + (1 + Xl + Xr | party_election),
                           data=losers,
                           warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love_economic)
summary(loser_love_economic)

loser_love_social <- brm(loveprop ~ post*populist_present + post*social_polarization
                         + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                         + Xl + Xr 
                         + (1 + Xl + Xr | party_election),
                         data=losers,
                         warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love_social)
summary(loser_love_social)




##################
# losers + angry #
##################

loser_angry_economic <- brm(angryprop ~ post*populist_present + post*economic_polarization
                            + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                            + Xl + Xr 
                            + (1 + Xl + Xr | party_election),
                            data=losers,
                            warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry_economic)
summary(loser_angry_economic)

loser_angry_social <- brm(angryprop ~ post*populist_present + post*social_polarization
                          + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                          + Xl + Xr 
                          + (1 + Xl + Xr | party_election),
                          data=losers,
                          warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry_social)
summary(loser_angry_social)

#save(winner_love_economic, winner_love_social, 
#     winner_angry_economic, winner_angry_social,
#     loser_love_economic, loser_love_social,
#     loser_angry_economic, loser_angry_social,
#     file="Stan_TwoPolarization.RData")




##################
# result summary #
##################

load("PSRM Replication Files/Stan_TwoPolarization.RData")

round(posterior_interval(loser_angry_economic, "post:economic_polarization", 0.9), 2)
round(posterior_interval(loser_angry_social, "post:social_polarization", 0.9), 2)

# plot interaction terms
getStdPostCI <- function(model, var1, var2, intr, label){
  coeftab <- summary(model)[[14]][,c(1,3,4)]
  coeftab <- coeftab[intr,]
  
  temp <- data.frame(label=label,
                     est=coeftab[1],
                     lwr=coeftab[2],
                     upr=coeftab[3])
  colnames(temp) <- c("label", "est", "lwr", "upr")
  return(temp)
}

citab <- rbind(getStdPostCI(winner_love_economic, 2, 4, 13, "Winner + Love"),
               getStdPostCI(winner_love_social, 2, 4, 13, "Winner + Love"),
               getStdPostCI(winner_angry_economic, 2, 4, 13, "Winner + Angry"),
               getStdPostCI(winner_angry_social, 2, 4, 13, "Winner + Angry"),
               getStdPostCI(loser_love_economic, 2, 4, 13, "Loser + Love"),
               getStdPostCI(loser_love_social, 2, 4, 13, "Loser + Love"),
               getStdPostCI(loser_angry_economic, 2, 4, 13, "Loser + Angry"),
               getStdPostCI(loser_angry_social, 2, 4, 13, "Loser + Angry"))

citab$intr <- rep(c("(a) Post Election × Economic Polarization", 
                    "(b) Post Election × Social Polarization"),
                  times=4)

citab$label <- factor(citab$label, levels=rev(unique(citab$label)))
citab$intr <- factor(citab$intr, levels=unique(citab$intr))

# figure O.1
ggplot(data = citab, 
       aes(x = est, y = label)) + 
  facet_wrap(. ~ intr, ncol = 1) +
  geom_errorbar(data = citab, aes(xmin = lwr, xmax = upr),
                lwd=0.8, width = 0.1) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_point(cex=4, pch=19) +
  xlab("Posterior Mean and 95% Credible Interval") +
  ylab("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12),
        panel.spacing = unit(1, "lines"))

#ggsave("twopolarization.pdf", width=8, height=8)


# correlations
pols <- rbind(winners[,c("polarization", "economic_polarization", "social_polarization")],
              losers[,c("polarization", "economic_polarization", "social_polarization")])

# table O.1
stargazer::stargazer(cor(pols, use="pairwise.complete.obs"), digits = 2)
